package com.wyw.learning.thread.future;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 演示一个Future的使用方法1
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class OneFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
        Future<Integer> future = service.submit(new CallableTask());
        System.out.println(future.get());//等待结果时不是一直自旋，而是等待被唤醒，不浪费CPU
        service.shutdown();
    }

    static class CallableTask implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            Thread.sleep(3*1000);
            return new Random().nextInt();
        }
    }
}
